package com.learncloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learncloud.common.core.exception.ServiceException;
import com.learncloud.entity.VideoDramasSeries;
import com.learncloud.entity.VideoInfo;
import com.learncloud.mapper.VideoDramasSeriesMapper;
import com.learncloud.service.IVideoDramasSeriesService;
import com.learncloud.service.IVideoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 视频集数表 服务实现类
 * </p>
 *
 * @author yj
 */
@Service
public class VideoDramasSeriesServiceImpl extends ServiceImpl<VideoDramasSeriesMapper, VideoDramasSeries> implements IVideoDramasSeriesService {

    @Autowired
    private IVideoInfoService iVideoInfoService;

    @Override
    public void release(Integer id) {
        VideoDramasSeries byId = this.getById(id);
        if (byId == null){
            throw new ServiceException("该集不存在");
        }
        if (byId.getStatus().equals("0")){ // 取消发布
            byId.setStatus("1");
            // 所有集数都取消发布，则取消发布视频
            int count = this.count(new QueryWrapper<>(new VideoDramasSeries().setStatus("0").setVideoInfoId(byId.getId())));
            if (count == 1){
                VideoInfo videoInfo = iVideoInfoService.getById(byId.getVideoInfoId());
                if ("0".equals(videoInfo.getIsRelease())){
                    videoInfo.setIsRelease("1");
                    iVideoInfoService.updateById(videoInfo);
                }
            }
        }else { // 发布
            byId.setStatus("0");
            // 有集数都取消发布且视频未发布，则发布视频
            VideoInfo videoInfo = iVideoInfoService.getById(byId.getVideoInfoId());
            if ("1".equals(videoInfo.getIsRelease())){
                videoInfo.setIsRelease("0");
                iVideoInfoService.updateById(videoInfo);
            }

        }
        this.updateById(byId);
    }
}
